package com.mmy.controller.scenic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.scenic.TScenicBase;
import com.mmy.domain.scenic.TScenicOrder;
import com.mmy.domain.scenic.VScenicBase;
import com.mmy.domain.scenic.VScenicOrder;
import com.mmy.domain.scenic.VScenicPlan;
import com.mmy.domain.sys.TSysOrg;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.sys.SysOrgServiceImpl;
import com.mmy.service.scenic.impl.ScenicBaseServiceImpl;
import com.mmy.service.scenic.impl.ScenicOrderServiceImpl;
import com.mmy.service.scenic.impl.ScenicPlanServiceImpl;
import com.mmy.service.scenic.impl.TScenicBaseServiceImpl;
import com.mmy.service.scenic.impl.TScenicOrderServiceImpl;
import com.mmy.service.scenic.impl.TScenicPlanServiceImpl;

/**
 * 类的介绍：景点信息
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class ScenicBaseController {

	@Autowired
	ScenicBaseServiceImpl scenicBaseServiceImpl;
	@Autowired
	ScenicPlanServiceImpl scenicPlanServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	ScenicOrderServiceImpl scenicOrderServiceImpl;
	@Autowired
	SysOrgServiceImpl sysOrgServiceImpl;
	@Autowired
	TScenicOrderServiceImpl tScenicOrderServiceImpl;
	@Autowired
	TScenicPlanServiceImpl tScenicPlanServiceImpl;
	@Autowired
	TScenicBaseServiceImpl tScenicBaseServiceImpl;

	/**
	 * 函数功能说明： 跳转 进入后台景区电子票信息页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2020.12.24
	 */
	@RequestMapping(value = "/scenicbaseindex")
	public String toScenicBase(HttpServletRequest request, String pageNumber, String pageSize, Model model,
			HttpSession session) {

		String strScenicName = request.getParameter("strScenicName");
		String strScenicContact = request.getParameter("strScenicContact");
		String strScenicTel = request.getParameter("strScenicTel");
		String strProvinceID = request.getParameter("strProvinceID");
		String strCityID = request.getParameter("strCityID");
		String strZoneID = request.getParameter("strZoneID");
		// 预留字段1 改为OrgID
		// 判断登录人 如果为超级管理员 查询所有 如果为其他有权限的员工只能查看到自己公司的景点信息
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		List<VScenicBase> lstScenicBase = scenicBaseServiceImpl.findByOrgID(pageNumber, pageSize, strScenicName,
				strScenicContact, strScenicTel, strProvinceID, strCityID, strZoneID, sessionSysUser);
		for (VScenicBase vScenicBase : lstScenicBase) {
			Integer iZoneID = vScenicBase.getZoneID();
			String province = constZoneServiceImpl.findProvinceStr(iZoneID);
			String city = constZoneServiceImpl.findCityStr(iZoneID);
			vScenicBase.setZoneName(province + city + vScenicBase.getZoneName());
		}
		model.addAttribute("lstScenicBase", lstScenicBase);
		model.addAttribute("lstScenicBase", lstScenicBase);
		HashMap<String, Integer> mapPage = scenicBaseServiceImpl.mapScenicBase(pageNumber, pageSize, strScenicName,
				strScenicContact, strScenicTel, strProvinceID, strCityID, strZoneID, sessionSysUser);
		// 查询所有的省
		List<TConstZone> lstConstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstConstZoneProvince", lstConstZoneProvince);
		if (!(strProvinceID == "" || strProvinceID == null)) {
			// 根据省ID查出该省所有的市
			List<TConstZone> lstConstZoneCity = constZoneServiceImpl.findCities(Integer.parseInt(strProvinceID));
			model.addAttribute("lstConstZoneCity", lstConstZoneCity);
		}
		if (!(strCityID == "" || strCityID == null)) {
			// 根据省ID查出该市所有的区
			List<TConstZone> lstConstZoneDistrict = constZoneServiceImpl.findDistricts(Integer.parseInt(strCityID));
			model.addAttribute("lstConstZoneDistrict", lstConstZoneDistrict);
		}
		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		model.addAttribute("strScenicName", strScenicName);
		model.addAttribute("strScenicContact", strScenicContact);
		model.addAttribute("strScenicTel", strScenicTel);
		model.addAttribute("strProvinceID", strProvinceID);
		model.addAttribute("strCityID", strCityID);
		model.addAttribute("strZoneID", strZoneID);
		return "webbackground/scenicmanagement/ScenicBaseIndex";

	}

	/**
	 * 函数功能说明： 跳转 进入后台景区电子票详情信息页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2020.12.24
	 */
	@RequestMapping(value = "/scenicbaseinfo")
	public String toScenicBaseInfo(HttpServletRequest request, Model model) {

		VScenicBase dbScenicBase = scenicBaseServiceImpl.findByScenicID(request.getParameter("iScenicID"));
		model.addAttribute("dbScenicBase", dbScenicBase);
		List<VScenicPlan> lstScenicPlan = scenicPlanServiceImpl.getByScenicID(request.getParameter("iScenicID"));
		model.addAttribute("lstScenicPlan", lstScenicPlan);
		// 通过所属区域查询省和市
		String strProvince = constZoneServiceImpl.findProvince(dbScenicBase.getZoneID()).getZoneName();
		String strCity = constZoneServiceImpl.findCity(dbScenicBase.getZoneID()).getZoneName();
		model.addAttribute("strProvince", strProvince);
		model.addAttribute("strCity", strCity);
		// 查询景点所属公司名称
		TSysOrg dbSysOrg = sysOrgServiceImpl.findByOrgID(Integer.parseInt(dbScenicBase.getSpare1()));
		model.addAttribute("strOrgName", dbSysOrg.getOrgName());
		return "webbackground/scenicmanagement/ScenicBaseInfo";
	}

	/**
	 * 函数功能说明： 跳转 进入后台景区创建信息页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2020.12.24
	 */
	@RequestMapping(value = "/tocreatescenicbase")
	public String toCreateScenicBase(Model model) {

		List<TConstZone> lstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstZoneProvince", lstZoneProvince);
		return "webbackground/scenicmanagement/CreateScenicBase";
	}

	/**
	 * 函数功能说明： 新建景点信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间2020.12.24
	 */

	@RequestMapping(value = "/savecreatescenicbase")
	public String saveCreateScenicBase(TScenicBase tScenicBase, MultipartFile fScenicImg, HttpSession session) {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		if (scenicBaseServiceImpl.createScenicBase(tScenicBase, fScenicImg, sessionSysUser) == true) {
			return "redirect:scenicbaseindex";
		} else {
			return "Test";
		}
	}

	/**
	 * 函数功能说明：后台 进入修改景点信息页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2020.12.25
	 */

	@RequestMapping(value = "/toupdatescenicbase")
	public String toUpdateScenicBase(HttpServletRequest request, Model model) {

		VScenicBase dbScenicBase = scenicBaseServiceImpl.findByScenicID(request.getParameter("iScenicID"));
		model.addAttribute("dbScenicBase", dbScenicBase);
		// 通过目的地查询省和市
		int iProvinceID = constZoneServiceImpl.findProvince(dbScenicBase.getZoneID()).getZoneID();
		String strCity = constZoneServiceImpl.findCity(dbScenicBase.getZoneID()).getZoneName();
		// 查询所有区域省市区
		List<TConstZone> lstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstZoneProvince", lstZoneProvince);
		model.addAttribute("iProvinceID", iProvinceID);
		model.addAttribute("strCity", strCity);
		return "webbackground/scenicmanagement/UpdateScenicBase";

	}

	/**
	 * 函数功能说明：后台 修改景点信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2020.12.25
	 */
	@RequestMapping(value = "/saveupdatescenicbase")
	public String saveUpdateScenicBase(TScenicBase tScenicBase, MultipartFile fScenicImg, HttpSession session)
			throws Exception {

		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		scenicBaseServiceImpl.updateScenic(tScenicBase, fScenicImg, sessionSysUser);
		return "redirect:scenicbaseindex";

	}

	/**
	 * 函数功能说明： 删除景点信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改日期2020.12.25
	 */

	@PostMapping("/delscenicbase")
	@ResponseBody
	public Map<String, Object> delScenicBase(String checkList) {

		Map<String, Object> map = scenicBaseServiceImpl.delScenicBase(checkList);

		return map;
	}

	/* 前台 */
	/**
	 * 函数功能说明： 跳转进入前台景点页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人Hanxl 修改人Hanxl
	 * @date 创建日期2020.09.12 修改时间 2020.12.30
	 */

	@RequestMapping(value = "/tofrontscenicindex")
	public String toFrontScenicIndex(HttpServletRequest request, Model model, HttpSession session, String pageNumber,
			String pageSize) {
		// 获取当前定位
		String strCityName = (String) session.getAttribute("sessionCity");
		if (strCityName == null) {
			strCityName = "上海市";
		}
		TConstZone dbConstZone = new TConstZone();
		// 搜索条件
		String strScenicName = request.getParameter("strScenicName");
		String strProvinceID = request.getParameter("strProvinceID");
		String strCityID = request.getParameter("strCityID");
		String strScenicPrice1 = request.getParameter("strScenicPrice1");
		String strScenicPrice2 = request.getParameter("strScenicPrice2");
		if (strCityID == null || strCityID == "") {
			// 查询当前定位的ZoneID
			dbConstZone = constZoneServiceImpl.getZoneID(strCityName);
		} else {
			dbConstZone = constZoneServiceImpl.findByZoneID(strCityID);
		}

		// 查询该市的所有景区

		// 查询计划日期大于当前日期的景区ID 价格区间在搜索范围的
		List<Integer> lstScenicID = scenicPlanServiceImpl.getScenicID(strScenicPrice1, strScenicPrice2);
		List<VScenicBase> lstScenicBase = scenicBaseServiceImpl.findBySearch(pageNumber, pageSize, lstScenicID,
				dbConstZone.getZoneID(), strScenicName);

		for (VScenicBase vScenicBase : lstScenicBase) {
			Integer iZoneID = vScenicBase.getZoneID();
			String province = constZoneServiceImpl.findProvinceStr(iZoneID);
			String city = constZoneServiceImpl.findCityStr(iZoneID);
			vScenicBase.setZoneName(province + city + vScenicBase.getZoneName());
		}
		model.addAttribute("lstScenicBase", lstScenicBase);
		// 当前时间有效的计划最小价格
		List<VScenicPlan> lstScenicPlan = scenicPlanServiceImpl.getMinPrice();
		model.addAttribute("lstScenicPlan", lstScenicPlan);
		/* 查询所有区域 国家或者省市区 */
		List<TConstZone> lstConstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstConstZoneProvince", lstConstZoneProvince);
		HashMap<String, Integer> mapPage = scenicBaseServiceImpl.mapFrontScenicBase(pageNumber, pageSize, lstScenicID);

		model.addAttribute("iPageNo", mapPage.get("iPageNo"));
		model.addAttribute("iTotalCount", mapPage.get("iTotalCount"));
		model.addAttribute("iMaxPage", mapPage.get("iMaxPage"));
		model.addAttribute("iPageSize", mapPage.get("iPageSize"));
		if (!(strProvinceID == "" || strProvinceID == null)) {
			// 根据省ID查出该省所有的市
			List<TConstZone> lstConstZoneCity = constZoneServiceImpl.findCities(Integer.parseInt(strProvinceID));
			model.addAttribute("lstConstZoneCity", lstConstZoneCity);
		}

		model.addAttribute("strScenicName", strScenicName);
		model.addAttribute("strProvinceID", strProvinceID);
		model.addAttribute("strCityID", strCityID);
		model.addAttribute("strScenicPrice1", strScenicPrice1);
		model.addAttribute("strScenicPrice2", strScenicPrice2);
		return "scenic/ScenicIndex";

	}

	/**
	 * 函数功能说明： 跳转 进入前台景区电子票详情信息页面
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2020.12.24
	 */
	@RequestMapping(value = "/tofrontscenicbaseinfo")
	public String toFrontScenicBaseInfo(HttpServletRequest request, Model model) {

		VScenicBase dbScenicBase = scenicBaseServiceImpl.findByScenicID(request.getParameter("iScenicID"));
		model.addAttribute("dbScenicBase", dbScenicBase);
		List<VScenicPlan> lstScenicPlan = scenicPlanServiceImpl.getByScenicID(request.getParameter("iScenicID"));
		model.addAttribute("lstScenicPlan", lstScenicPlan);
		// 通过所属区域查询省和市
		String strProvince = constZoneServiceImpl.findProvince(dbScenicBase.getZoneID()).getZoneName();
		String strCity = constZoneServiceImpl.findCity(dbScenicBase.getZoneID()).getZoneName();
		model.addAttribute("strProvince", strProvince);
		model.addAttribute("strCity", strCity);
		// 查询景点所属公司名称
		TSysOrg dbSysOrg = sysOrgServiceImpl.findByOrgID(Integer.parseInt(dbScenicBase.getSpare1()));
		model.addAttribute("strOrgName", dbSysOrg.getOrgName());
		return "scenic/ScenicBaseInfo";
	}

	@RequestMapping(value = "/orderscenicinfo")
	public String ToOrderScenicInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TScenicOrder tScenicOrder = tScenicOrderServiceImpl.findByID(iID);
		model.addAttribute("tScenicOrder", tScenicOrder);

		String strOrderID = tScenicOrder.getOrderID();
		int iPlanID = Integer.parseInt(strOrderID.substring(6, 12));

		VScenicPlan vScenicPlan = tScenicPlanServiceImpl.findByID(iPlanID);

		model.addAttribute("vScenicPlan", vScenicPlan);

		return "webbackground/scenic/orderscenicinfo";
	}

	@RequestMapping(value = "/orderscenic")
	public String ToOrderScenic(HttpServletRequest request, Model model) {

		int iScenicID = Integer.parseInt(request.getParameter("iScenicID"));

		List<VScenicOrder> lstScenicOrder = tScenicOrderServiceImpl.findByScenicID(iScenicID);
		model.addAttribute("orderlist", lstScenicOrder);
		model.addAttribute("iScenicID", iScenicID);

		return "webbackground/scenic/orderscenic";
	}

//收款打印页面
	@RequestMapping(value = "/printscenic")
	public String ToPrintScenic(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TScenicOrder tScenicOrder = tScenicOrderServiceImpl.findByID(iID);
		model.addAttribute("tScenicOrder", tScenicOrder);

		return "webbackground/scenic/printscenic";
	}

	// 退款打印页面
	@RequestMapping(value = "/printcancelscenic")
	public String ToPrintCancelScenic(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TScenicOrder tScenicOrder = tScenicOrderServiceImpl.findByID(iID);
		model.addAttribute("tScenicOrder", tScenicOrder);

		return "webbackground/scenic/printcancelscenic";
	}

	/**
	 * 函数功能说明： 查询景点信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/searchscenicbase")
	public String FindScenicBase(HttpServletRequest request, Model model) {

		String strScenicName = request.getParameter("ScenicName");
		if (strScenicName == "") {
			List<VScenicBase> lstScenicBase = tScenicBaseServiceImpl.findAll();
			model.addAttribute("slist", lstScenicBase);
			request.setAttribute("strScenicName", strScenicName);
			return "webbackground/scenic/scenicbase";
		} else {
			List<VScenicBase> lstScenicBase = tScenicBaseServiceImpl.findByNamel(strScenicName);
			model.addAttribute("slist", lstScenicBase);
			request.setAttribute("strScenicName", strScenicName);
			return "webbackground/scenic/scenicbase";
		}
	}
	
	/**
	  * @descr 级联选择器,本地景点 
	  * @param 
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-6-21
	 */
	@RequestMapping("/getScenicsByZone")
	@ResponseBody
	public String getScenicsByOrg(HttpServletRequest request,HttpSession session) {
		
		//TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		//int iOrgID = tSysUser.getOrgID();
		int iZoneID = Integer.parseInt(request.getParameter("iZoneID"));
		String res = scenicBaseServiceImpl.scenicSelect(iZoneID);
		
		return res;
	}

}
